// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
module.exports = {
	_before: function() { // 通用预处理器

	},
	/**
	 * 获取笔记列表
	 * @param {number} pageNo 页码
	 * @param {number} pageSize 每页条数
	 * @returns {Array} 数组
	 */
	async opendb_note_list(pageNo, pageSize) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		});
		const dbCmd = dbJQL.command
		const $ = dbCmd.aggregate
		return await dbJQL.collection('opendb-note').aggregate()
			.match({
				user_id: dbJQL.getCloudEnv("$cloudEnv_uid"),
				flag: false
			})
			.lookup({
				from: 'questions-topic',
				let: {
					topic_id: '$topic_id'
				},
				pipeline: $.pipeline().match(dbCmd.expr($.eq(["$_id", "$$topic_id"]))).done(),
				as: 'topic'
			})
			.project({
				content: 1,
				topic: $.arrayElemAt(['$topic', 0])
			})
			.skip((pageNo - 1) * pageSize).limit(pageSize).end()
	},
	/**
	 * 删除笔记
	 * @param {string} id 笔记id
	 * @returns {object} 结果
	 */
	async opendb_note_delete(id) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		});
		return await dbJQL.collection('opendb-note').doc(id).remove()
	},
	/**
	 * 添加笔记
	 * @param {Array,object} data 笔记数据
	 * @returns {object} 返回值描述
	 */
	async opendb_note_add(data) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		});
		let arrys = []
		let reference = dbJQL.collection('opendb-note')
		data.map(item => {
			let {
				_id,
				...data
			} = item
			if (_id) {
				arrys.push(reference.doc(_id).update(data))
			} else {
				arrys.push(reference.add(data))
			}
		})
		return await Promise.all(arrys)
	},
	/**
	 * 笔记详情
	 * @param {string} _id 笔记
	 * @returns {object} 返回值描述
	 */
	async opendb_note_detail(_id) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		});
		return await dbJQL.collection('opendb-note').doc(_id).get({
			getOne: true
		})
	},
	/**
	 * 所有笔记
	 * @returns {Array} 数组
	 */
	async opendb_note_all() {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		});
		const dbCmd = dbJQL.command
		const $ = dbCmd.aggregate
		return await dbJQL.collection('opendb-note').aggregate().match({
				flag: false,
				user_id: dbJQL.getCloudEnv('$cloudEnv_uid')
			})
			.lookup({
				from: 'questions-topic',
				let: {
					topic_id: '$topic_id'
				},
				pipeline: $.pipeline().match(dbCmd.expr($.eq(["$_id", "$$topic_id"])))
					.lookup({
						from: 'topic-options',
						let: {
							topic_id: '$_id'
						},
						pipeline: $.pipeline().match(
							dbCmd.expr($.eq(['$topic_id', '$$topic_id']))
						).done(),
						as: 'options'
					})
					.lookup({
						from: 'opendb-note',
						let: {
							topic_id: '$_id'
						},
						pipeline: $.pipeline().match(
							dbCmd.expr($.and([
								$.eq(['$topic_id', '$$topic_id']),
								$.eq(['$flag', false]),
								$.eq(['$user_id', dbJQL.getCloudEnv('$cloudEnv_uid')])
							]))
						).done(),
						as: 'note'
					})
					.project({
						analysis: 1,
						answer: 1,
						difficulty: 1,
						options: 1,
						question_content: 1,
						question_storehouse_id: 1,
						question_type: 1,
						note: $.arrayElemAt(['$note', 0])
					})
					.done(),
				as: 'topic'
			})
			.project({
				topic: $.arrayElemAt(['$topic', 0])
			})
			.end()
	}
}